package com.resifind.www.dao.impl;

import com.resifind.www.dao.FavourDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.Merchant;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class FavourDaoImpl implements FavourDao {

    @Override
    public boolean queryFavourExist(int clientId, int merchantId) {
        //language=MySQL
        String sql = "select count(*) from resi_find.favour where client_id = ? and merchant_id = ?";
        Object[] params = {clientId, merchantId};
        List<Integer> query = JdbcUtils.query(sql, params, rs -> rs.getInt(1));
        return query.getFirst() > 0;
    }

    @Override
    public boolean favourMerchant(int clientId, int merchantId, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.favour (client_id, merchant_id) values (?, ?)";
        Object[] params = {clientId, merchantId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public List<Merchant> queryFavourMerchant(int clientId) {
        //language=MySQL
        String sql = """
                select m.*, 0 as is_favour from resi_find.merchant m
                join resi_find.favour f on m.id = f.merchant_id
                where f.client_id = ?
                """;
        Object[] params = {clientId};
        return Mappers.merchantMapper(sql, params);
    }

    @Override
    public boolean dropFavour(int clientId, int merchantId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.favour where client_id = ? and merchant_id = ?";
        Object[] params = {clientId, merchantId};
        return JdbcUtils.update(sql, params, conn);
    }
}
